package com.oa.service.impl;

import com.oa.bean.Dept;
import com.oa.bean.Emp;
import com.oa.dao.DeptDao;
import com.oa.dao.EmpDao;
import com.oa.service.EmpService;
import com.oa.utils.PageBean;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;

    @Autowired
    private DeptDao deptDao;

    @Override
    public PageBean pageQuery(PageBean pageBean) {
        return empDao.pagingQuery(pageBean);
    }

    @Override
    public void save(Emp emp) {


        //替换主管
        if (emp.getFlag() == 1) {
            DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);
            dc.add(Restrictions.eq("flag", 1));
            dc.add(Restrictions.eq("dept.id", emp.getDept().getId()));
            Emp reemp = empDao.findByCriteria(dc);
            if (reemp != null)
                reemp.setFlag(0);
        }

        empDao.save(emp);

        // 有多对一关系时，注意保存的顺序！！
        Dept dept = deptDao.findById(emp.getDept().getId());


        dept.getEmps().add(emp);

        emp.setDept(dept);

    }

    @Override
    public void departEmp(Emp emp) {

        Emp remp = empDao.findById(emp.getId());

        remp.getDept().getEmps().remove(remp);

        remp.setDept(null);

        remp.setIs(0);

        empDao.saveOrUpdate(remp);


    }

    @Override
    public String checkCanDepart(Integer id) {

        Emp emp = empDao.findById(id);

        //检查是否能离职，是否存在有未交接的客户
        if (emp.getCustomers().size() > 0) {
            return "false";
        }

        return "true";
    }

    @Override
    public Emp beforeEditEmp(Integer id) {
        return empDao.findById(id);
    }

    //判断是否有人使用此图片
    public boolean canDelImag(String lastpath) {
        DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);
        dc.add(Restrictions.eq("img", lastpath));
//        System.out.println(lastpath);
        Emp emp = empDao.findByCriteria(dc);
        if (emp != null) {
            return false;
        }
        return true;
    }

    @Override
    public boolean restoreEmp(Emp model) {

        Emp emp = empDao.findById(model.getId());

        Dept dept = deptDao.findById(model.getDept().getId());

        dept.getEmps().add(emp);

        emp.setDept(dept);

        emp.setIs(1);

        empDao.update(emp);

        return true;
    }

    @Override
    public List<Emp> findAllNames(DetachedCriteria dc) {

        List<Emp> list = empDao.findByCriteriaReturnList(dc);

        if (list != null && list.size() > 0) {
            return list;
        }

        return null;
    }

    @Override
    public void updateEmp(Emp model) {

        Integer id = model.getDept().getId();
        Dept dept = deptDao.findById(id);

        Integer flag = model.getFlag();

        if (flag == 1) {//替换主管
            DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);
            dc.add(Restrictions.eq("flag", 1));
            dc.add(Restrictions.eq("dept.id", dept.getId()));
            Emp reemp = empDao.findByCriteria(dc);
            if (reemp != null)
                reemp.setFlag(0);
        }

        Emp emp = empDao.findById(model.getId());
        emp.setDept(dept);
        emp.setAdmin(model.getAdmin());
        emp.setFlag(model.getFlag());//设置正常值
        emp.setImg(model.getImg());
        emp.setLoginname(model.getLoginname());
        emp.setPsw(model.getPsw());
        emp.setRemark(model.getRemark());
        emp.setSex(model.getSex());
        emp.setTruename(model.getTruename());
        emp.setIs(1);

        empDao.update(emp);
    }

    @Override
    public Emp findEmpById(Integer id) {
        return empDao.findById(id);
    }

    @Override
    public Integer validateLoginname(String loginname) {

        DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);
        dc.add(Restrictions.eq("loginname", loginname));
        Emp emp = empDao.findByCriteria(dc);
        if (emp != null) {
            return 1;
        }
        return 0;
    }

}
